package com.xdw.logindemoforjava.slice;

import com.xdw.logindemoforjava.MainAbility2;
import com.xdw.logindemoforjava.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class LoginAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private Button btnLogin;
    private TextField tfUsername, tfPassword;
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, "MainAbilitySlice.class");
    public static final int REQUEST_CODE_MAINABILITY=1;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_login);
        btnLogin = findComponentById(ResourceTable.Id_btnLogin);
        tfUsername = findComponentById(ResourceTable.Id_username);
        btnLogin.setClickedListener(this);
        /*btnLogin.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.info(LABEL_LOG,  "click");
                new ToastDialog(MainAbilitySlice.this).setText("点击了登录按钮").setDuration(2000).show();
//                new ToastDialog(MainAbilitySlice.this).setText("点击登录").setDuration(2000).show();
//                intent.setParam("username","夏德旺");
                //present(new OtherAbilitySlice(),intent);
                //presentForResult(new OtherAbilitySlice(),intent,1);
                *//*Operation operation = new Intent.OperationBuilder()
                        .withAbilityName(MainAbility2.class)
                        .withBundleName("com.xdw.szdxclass")
                        .build();
                intent.setOperation(operation);
                startAbility(intent);*//*
            }
        });*/
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        HiLog.info(LABEL_LOG, "click");
        new ToastDialog(this).setText("点击了登录按钮").setDuration(2000).show();
        // present(new MainAbilitySlice(),new Intent()); 无传参跳转
        //带参数跳转
        Intent intent = new Intent();
        intent.setParam("username","夏德旺");
        //present(new MainAbilitySlice(),intent);
        //presentForResult(new MainAbilitySlice(),intent,REQUEST_CODE_MAINABILITY);
        intent.setOperation(new Intent.OperationBuilder().withAbilityName(MainAbility2.class).withBundleName("com.xdw.szdxclass").build());
        startAbility(intent);
    }

  /*  @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        if (requestCode==1){
            tfUsername.setText(resultIntent.getStringParam("result"));
        }
    }*/

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        if(requestCode==REQUEST_CODE_MAINABILITY){
            new ToastDialog(this).setText(resultIntent.getStringParam("result")).setDuration(2000).show();
        }
    }
}
